<?php

namespace App\Http\Controllers;


use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Gregwar\Captcha\CaptchaBuilder;
use Session;
use App\Pro;
use App\Att;
class ProjectController extends Controller

{



    public function jie() {



        return view('woyaojiekuan');

    }
    public function jiePost(Request $req) {
        $this->validate($req,[
            'money'=> 'required|digits_between:4,10',
            'mobile'=> 'required|regex:/^1[34578]\d{9}$/',
            'age'=> 'required|in:15,40,80',
        ],[
            'required'=> ':attribute 不能为空!',
            'digits_between'=> ':attribute 是:min到:max位的正整数',
            'mobile.regex'=> '手机号不无效',
            'in'=> ':attribute 年龄只能是:values 的其中之一'
        ]);
        $input = Input::all();
        if(Session::get('milkcaptcha') !=  $input['captcha']) {
            return back()->with('msg','图形验证码不正确！');
        }

        $pro = new Pro();
        $attr = new Att();
        //写入主表
        $pro->money = intval($req->money) *100;
        $pro->mobile = $req->mobile ? $req->mobile : '';
        $pro->name = $req->user()->name;
        $pro->uid = $req->user()->uid;
        $pro->age = $req->age;
        $pro->pubtime = time();
        $pid = $pro->save();

        //写入附属表
        $attr->pid = $pid;
        $attr->uid = $req->user()->uid;
        $aid = $attr->save();
        if ($aid && $pid)
        return ['code'=>1,'meg'=>'借款申请成功，等待管理员审核！'];
        return ['code'=>0,'meg'=>'借款申请失败！稍后重试！'];


    }

    public function getCaptcha() {
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();


        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //var_dump(Session::get('milkcaptcha'));
       // $_SESSION['code'] = $phrase;
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

    }
}
